const app = getApp()
Page({
  data: {
    currentTab: 0,
    courseLibraryList: [],
    coachingSessionId: '',
    coachDetail: {},
    roleType: '',
    hiddenModal: true,
    rejectReason: "",
    mainHidden: true
  },
  onLoad: function (options) {
    const userInfo = JSON.parse(wx.getStorageSync('userInfo'))
    let roletypeIfmange = userInfo.roleType === 'admin-admin' ? '1' : '2'
    this.setData({
      coachingSessionId: app.globalData.coachingSessionId || '1849242289428234240',
      roleType: roletypeIfmange
    })
    // 页面初始化 options为页面跳转所带来的参数
  },
  onShow: function () {
    this.getDetail()
  },
  //删除项目
  deleteItem() {
    let data = {
      coachingSessionId: this.data.coachingSessionId
    }
    wx.showModal({
      title: `删除提醒`,
      content: `删除此考试后数据不可恢复，确认删除吗？`,
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      success: (res) => {
        if (res.confirm) {
          app.api.post({
            url: 'coaching_vx_admin_session_delete',
            data
          }).then(res => {
            console.log(res)
            wx.showToast({
              title: '操作成功',
              icon: 'success',
              duration: 1000
            })
            wx.navigateTo({
              url: '/myself/pages/train-manage/index/index'
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getDetail() {
    let url = `/coaching/vx/admin/session/${this.data.coachingSessionId}/detail4View`
    console.log(url)
    app.api.get({
      url: 'coaching_vx_admin_session_detail4View',
      data: {
        coachingSessionId: this.data.coachingSessionId
      }
    }).then(res => {
      console.log(res)
      let obj = res || {}
      obj.status = res.activityStatusLabel
      this.setData({
        coachDetail: obj
      })
    })
  },
  //编辑按钮
  editItem() {
    app.globalData.coachingSessionId = this.data.coachingSessionId
    app.globalData.operateType = 'edit'
    app.globalData.trainInfo = {}
    wx.navigateTo({
      url: '/myself/pages/train-manage/courseschose/courseschose'
    })
  },

  //审核通过
  checkPass() {
    let that = this
    wx.showModal({
      title: `审核通过`,
      content: `审核通过后培训活动将默认为上架状态，确认审核通过吗？`,
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      success (res) {
        if (res.confirm) {
          app.api.post({
            url: 'coaching_vx_admin_session_audit_pass',
            data: {
              coachingSessionId: that.data.coachingSessionId
            }
          }).then((res) => {
            that.getDetail()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //审核不通过
  rejectModal() {
    this.setData({
      hiddenModal: false
    })
  },
  //取消
  rejectCancel() {
    this.setData({
      hiddenModal: true,
      rejectReason: ''
    })
  },
  //审核不通过
  checkReject() {
    if (!this.data.rejectReason) {
      wx.showToast({
        title: '不通过理由必填',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    app.api.post({
      url: 'coaching_vx_admin_session_audit_reject',
      data: {
        rejectReason: this.data.rejectReason,
        coachingSessionId:this.data.coachingSessionId
      }
    }).then((res) => {
      wx.showToast({
        title: '操作成功',
        icon: 'success',
        duration: 1000
      })
      this.setData({
        rejectReason:'',
        hiddenModal:true
      })
      this.getDetail()
    })
  },
  manageItem(e){
    let coachingSessionId = e.currentTarget.dataset.coachingsessionid
    let shelfStatus = e.currentTarget.dataset.shelfstatus
    console.log(shelfStatus,coachingSessionId)
    let data = {coachingSessionId:coachingSessionId}
    let that = this
    let url = shelfStatus==='up'?'coaching_vx_admin_session_shelf_down':'coaching_vx_admin_session_shelf_up'
    wx.showModal({
      title: `${shelfStatus==='up' ? '下' : '上'}架提醒`,
      content: `${shelfStatus==='up' ? '下架后学员不可见此培训活动，确认下架吗？' : '上架后学员可见此培训活动，确认上架吗？'}`,
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      confirmText: `${shelfStatus==='up' ? '下' : '上'}架`,
      success (res) {
        if (res.confirm) {
          app.api.post({
            url: url,
            data
          }).then(res => {
            console.log(res)
            wx.showToast({
              title: '操作成功',
              icon: 'success',
              duration: 1000
            })
            that.getDetail()
            // wx.navigateBack({delta: 1})
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toggleMain () {
    this.setData({
      mainHidden: !this.data.mainHidden
    })
  }
})